home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ditroff / RCS / main.c,v < prev    next >
Encoding:
Text File  |  1991-06-10  |  12.5 KB  |  548 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    shirriff:1.1; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     90.07.11.14.31.36;  author ouster;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @#ifndef DEFPRT
  26. #define DEFPRT "lw"
  27. #endif
  28.  
  29. #define TROFF     "/sprite/cmds.$MACHINE/troff_p"
  30. #define LPR    "/sprite/cmds.$MACHINE/lpr"
  31. #define DTBL     "/sprite/cmds.$MACHINE/tbl"
  32. #define DEQN     "/sprite/cmds.$MACHINE/eqn"
  33. #define GRN     "/sprite/cmds.$MACHINE/grn"
  34. #define PIC     "/sprite/cmds.$MACHINE/pic"
  35. #define REFER     "/sprite/cmds.$MACHINE/refer"
  36. #define IDEAL     "/sprite/cmds.$MACHINE/ideal"
  37.  
  38. #define TMPFILE "/tmp/ditXXXXX"
  39.  
  40. static char tempfile[] = TMPFILE;
  41.  
  42. char    *printer = DEFPRT;    /* printer to use */
  43. char    *ditentry;        /* ditcap entry to use */
  44. char    *type;            /* type of printer */
  45. char    *font;            /* font directory */
  46.  
  47. #include <stdio.h>
  48. #define    NULL    0
  49. #define BUFSIZ    1024
  50. char    ditline[BUFSIZ];    /* variables needed to read the ditcap lines*/
  51. char    ditbuf[BUFSIZ/2];    /*  and the ditcap file  */
  52. char    *bp;
  53.  
  54. #define BLANK ""
  55. char    files[BUFSIZ] = BLANK;    /* files to print  */
  56. char    flags[BUFSIZ/4] = BLANK;/* troff options - pass to TROFF */
  57. char    spool[BUFSIZ/4] = BLANK;/* spooling options - pass to LPR  */
  58. char    line1[BUFSIZ] = BLANK;    /* lines to be executed   */
  59. char    line2[BUFSIZ] = BLANK;
  60. char    line3[BUFSIZ] = BLANK;
  61. char    type_font[BUFSIZ/4] = BLANK;
  62. char    temp[BUFSIZ] = BLANK;    /* temporary buffer */
  63.  
  64. char    *FD;            /* ditcap characteristics  */
  65. char    *TR;
  66. char    *LO;
  67. char    *F1;
  68. char    *F2;
  69. char    *F3;
  70. char    *F4;
  71. char    *OL;
  72. char    *FA;
  73. char    *OA;
  74. char    *FT;
  75. char    *OT;
  76. char    *FP;
  77. char    *OP;
  78. char    *PV;
  79. char    *LP;
  80. char    *TB;
  81. char    *EQ;
  82. char    *GR;
  83. char    *PI;
  84. char    *RF;
  85. char    *ID;
  86. char    *SE;
  87.  
  88.  
  89. main(argc, argv)
  90. int argc;
  91. char **argv;
  92. {
  93.     char *cp;
  94.  
  95.     int     gottype=0, gotjob=0, gotfont=0, gotdit=0;
  96.     int     t=0, a=0;
  97.     int    tbl=0, eqn=0, grn=0, seqn=0, pic=0, refer=0, ideal=0;
  98.     int    first=1;
  99.     int    debug = 0;
  100.  
  101.     char     *operand();
  102.     char     *getenv();
  103.     char     *gettype();
  104.     char    *mktemp();
  105.     int    strlen();
  106.     int    strcmp();
  107.  
  108.     if (cp=getenv("PRINTER")) printer = cp;
  109.     if (cp=getenv("TYPESETTER")) printer = cp;
  110.     ditentry = printer;
  111.  
  112.     while (--argc) {
  113.         if (**++argv != '-')    /* if not an option add to files */
  114.         sprintf(files,"%s %s",files,*argv);
  115.         else
  116.           switch ((*argv)[1]) {      /* process options  */
  117.  
  118.         case 'P':    /* final output typesetter name */
  119.             printer = operand(&argc, &argv);
  120.             if (!gotdit) ditentry = printer;
  121.             break;
  122.  
  123.         case 'T':    /* printer type */
  124.             type = operand(&argc, &argv);
  125.             gottype = 1;
  126.             break;
  127.  
  128.         case 'F':    /* font directory */
  129.             font = operand(&argc, &argv);
  130.             gotfont = 1;
  131.             break;
  132.  
  133.         case 'D':    /* ditcap entry */
  134.             ditentry = operand(&argc, &argv);
  135.             gotdit = 1;
  136.             break;
  137.  
  138.         case 't':    
  139.             if ((*argv)[2]) { 
  140.                 if (strcmp(*argv,"-tbl") == 0) 
  141.                     tbl = 1;             /* -tbl option */
  142.                 else                /* some other option */
  143.                     sprintf(flags,"%s %s",flags,*argv);
  144.             } else {                      /* must be -t option  */
  145.                 sprintf(flags,"%s %s",flags,*argv);
  146.                 t = 1;
  147.             }
  148.             break;
  149.  
  150.         case 'a':    /* ascii approximation to standard output  */
  151.             if ((*argv)[2]) { 
  152.                 sprintf(flags,"%s %s",flags,*argv);
  153.             } else {                      /* must be -a option  */
  154.                 sprintf(flags,"%s %s",flags,*argv);
  155.                 a = 1;
  156.             }
  157.             break;
  158.  
  159.         case 'm':    /* either -mmac TROFF option or
  160.                       -m spooling option   */
  161.             if ((*argv)[2]) 
  162.                 sprintf(flags,"%s %s",flags,*argv);
  163.             else
  164.                 sprintf(spool,"%s %s",spool,*argv);
  165.             break;
  166.  
  167.         case 'h':    /* spooling options  */
  168.             if ((*argv)[2]) 
  169.                 sprintf(flags,"%s %s",flags,*argv);
  170.             else
  171.                 sprintf(spool,"%s %s",spool,*argv);
  172.             break;
  173.  
  174.         case '#':    /* spooling options  */
  175.             sprintf(spool,"%s %s",spool,*argv);
  176.             break;
  177.  
  178.         case 'C':    /* change class name - to spooling options */
  179.             cp = operand(&argc, &argv);
  180.             sprintf(temp," -C%s%s",cp,spool);
  181.             strcpy(spool,temp);
  182.             break;
  183.  
  184.         case 'J':    /* change job name - to spooling options */
  185.             cp = operand(&argc, &argv);
  186.             sprintf(temp," -J%s%s",cp,spool);
  187.             strcpy(spool,temp);
  188.             gotjob = 1;
  189.             break;
  190.  
  191.         case 'e':    
  192.             if (strcmp(*argv,"-eqn") == 0) 
  193.                 eqn = 1;             
  194.             else                
  195.                 sprintf(flags,"%s %s",flags,*argv);
  196.             break;
  197.  
  198.         case 'd':    
  199.             if (strcmp(*argv,"-deqn") == 0) 
  200.                 eqn = 1;             
  201.             else if (strcmp(*argv,"-dtbl") == 0) 
  202.                 tbl = 1;             
  203.             else if (strcmp(*argv,"-debug") == 0) 
  204.                 debug = 1;             
  205.             else
  206.                 sprintf(flags,"%s %s",flags,*argv);
  207.             break;
  208.  
  209.         case 's':    
  210.             if (strcmp(*argv,"-seqn") == 0) {
  211.                 eqn = 1;             
  212.                 seqn = 1;             
  213.             } else                
  214.                 sprintf(flags,"%s %s",flags,*argv);
  215.             break;
  216.  
  217.         case 'g':    
  218.             if (strcmp(*argv,"-grn") == 0) 
  219.                 grn = 1;             
  220.             else                
  221.                 sprintf(flags,"%s %s",flags,*argv);
  222.             break;
  223.  
  224.         case 'p':    
  225.             if (strcmp(*argv,"-pic") == 0) 
  226.                 pic = 1;             
  227.             else                
  228.                 sprintf(flags,"%s %s",flags,*argv);
  229.             break;
  230.  
  231.         case 'r':    
  232.             if (strcmp(*argv,"-refer") == 0) 
  233.                 refer = 1;             
  234.             else                
  235.                 sprintf(flags,"%s %s",flags,*argv);
  236.             break;
  237.  
  238.         case 'i':    
  239.             if (strcmp(*argv,"-ideal") == 0) 
  240.                 ideal = 1;             
  241.             else                
  242.                 sprintf(flags,"%s %s",flags,*argv);
  243.             break;
  244.  
  245. /* here's the bug: if argument is just '-', it IS a file, i.e. stdin */
  246.         case '\0':
  247.             sprintf(files,"%s %s",files,*argv);
  248.             break;
  249. /* end of bug fix [as, 5/25/89] */
  250.  
  251.         default:    /* option to be passed to TROFF  */
  252.             sprintf(flags,"%s %s",flags,*argv);
  253.         }
  254.     }
  255.       
  256.     
  257.     if (!gotjob) {        /* make sure there is a jobname  */
  258.         sprintf(temp," -Jditroff%s",spool);
  259.         strcpy(spool,temp);
  260.     }
  261.  
  262.                 /* get the ditcap line from the ditcap  */
  263.                 /* file or environment variable  */
  264.     getditline(ditentry);
  265.  
  266.                 /* get the printer type for the given   */
  267.                 /*  ditcap entry from the ditcap file   */
  268.     if (!gottype)  type = gettype(ditentry); 
  269.                 /* get the options in the ditcap line   */
  270.     ditoptions();
  271.  
  272.  
  273.                 /* start building up the lines that we */
  274.                 /* will pass to UNIX to be executed    */
  275.                 /* according to */
  276.                 /* options and then execute the lines */
  277.  
  278.     if (seqn) {
  279.         if (SE == NULL) {
  280.              fprintf (stderr,"ditroff: se characteristic not defined in the ditcap file \n");
  281.              fprintf (stderr,"          no special equation characters defined\n");
  282.         } else if (strlen(files) == 0) {
  283.             fprintf (stderr,"ditroff: warning: ");
  284.             fprintf (stderr,"you can't use the -seqn option ");
  285.             fprintf (stderr,"when ditroff \n");
  286.             fprintf (stderr,"                  ");
  287.             fprintf (stderr,"is not given files as arguements \n");
  288.             fprintf (stderr,"                  ");
  289.             fprintf (stderr,"(ie. when the input is to come");
  290.             fprintf (stderr," from standard input) \n");
  291.         } else {
  292.              sprintf(temp," %s%s",SE,files);
  293.              strcpy(files,temp);
  294.         }
  295.     }
  296.  
  297.     sprintf(type_font," -T%s",type);
  298.     if (gotfont)
  299.         sprintf(type_font,"%s -F%s",type_font,font);
  300.     else if (FD != NULL)
  301.         sprintf(type_font,"%s -F%s",type_font,FD);
  302.  
  303.     if (refer) {
  304.         sprintf(line1,"%s%s|",RF,files);
  305.         first = 0;
  306.     }
  307.     if (tbl){
  308.         if (first)
  309.             sprintf(line1,"%s%s|",TB,files);
  310.         else
  311.             sprintf(line1,"%s%s|",line1,TB);
  312.         first = 0;
  313.     }
  314.     if (grn){
  315.         if (first)
  316.             sprintf(line1,"%s%s%s|",GR,type_font,files);
  317.         else
  318.             sprintf(line1,"%s%s%s|",line1,GR,type_font);
  319.         first = 0;
  320.     }
  321.     if (pic){
  322.         if (first)
  323.             sprintf(line1,"%s%s%s|",PI,type_font,files);
  324.         else
  325.             sprintf(line1,"%s%s%s|",line1,PI,type_font);
  326.         first = 0;
  327.     }
  328.     if (ideal){
  329.         if (first)
  330.             sprintf(line1,"%s%s%s|",ID,type_font,files);
  331.         else
  332.             sprintf(line1,"%s%s%s|",line1,ID,type_font);
  333.         first = 0;
  334.     }
  335.     if (eqn){
  336.         if (first)
  337.             sprintf(line1,"%s%s%s|",EQ,type_font,files);
  338.         else
  339.             sprintf(line1,"%s%s%s|",line1,EQ,type_font);
  340.         first = 0;
  341.     }
  342.  
  343.  
  344.                 /* add the end of the line depending  */
  345.                 /* on the options                     */
  346.  
  347.     if (a) {        /* ascii approximation   */
  348.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  349.         if (OA != NULL) sprintf(line1,"%s %s",line1,OA);
  350.         sprintf(line1,"%s%s",line1,flags);
  351.         if (first) sprintf(line1,"%s%s",line1,files);
  352.  
  353.         if (FA != NULL) sprintf(line1,"%s|%s",line1,FA);
  354.  
  355.         if (debug)
  356.                 printf("line1 is \n  %s \n",line1);
  357.         else
  358.             system(line1);
  359.  
  360.     }
  361.     else if (t) {          /* send to standard output  */
  362.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  363.         if (OT != NULL) sprintf(line1,"%s %s",line1,OT);
  364.         sprintf(line1,"%s%s",line1,flags);
  365.         if (first) sprintf(line1,"%s%s",line1,files);
  366.  
  367.         if (FT != NULL) sprintf(line1,"%s|%s",line1,FT);
  368.  
  369.         if (debug)
  370.                 printf("line1 is \n  %s \n",line1);
  371.         else
  372.             system(line1);
  373.  
  374.     }
  375.     else if (PV != NULL) {    /* use the previewer  */
  376.         cp = mktemp(tempfile);
  377.         strcpy(temp,cp);
  378.  
  379.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  380.         if (OP != NULL) sprintf(line1,"%s %s",line1,OP);
  381.         sprintf(line1,"%s%s",line1,flags);
  382.         if (first) sprintf(line1,"%s%s",line1,files);
  383.  
  384.         if (FP != NULL) sprintf(line1,"%s|%s",line1,FP);
  385.  
  386.         sprintf(line1,"%s > %s",line1,temp);
  387.         sprintf(line2,"%s %s",PV,temp);
  388.         sprintf(line3,"/sprite/cmds.$MACHINE/rm -f %s",temp);
  389.  
  390.         if (debug) {
  391.                 printf("line1 is \n  %s \n",line1);
  392.                 printf("line2 is \n  %s \n",line2);
  393.                 printf("line3 is \n  %s \n",line3); 
  394.         } else {
  395.             system(line1);
  396.             system(line2); 
  397.             system(line3);
  398.         }
  399.  
  400.     }
  401.     else {             /* standard troff to printer  */
  402.         sprintf(line1,"%s%s%s",line1,TR,type_font);
  403.         if (OL != NULL) sprintf(line1,"%s %s",line1,OL);
  404.         sprintf(line1,"%s%s",line1,flags);
  405.         if (first) sprintf(line1,"%s%s",line1,files);
  406.  
  407.         if (F1 != NULL) sprintf(line1,"%s|%s",line1,F1);
  408.         if (F2 != NULL) sprintf(line1,"%s|%s",line1,F2);
  409.         if (F3 != NULL) sprintf(line1,"%s|%s",line1,F3);
  410.         if (F4 != NULL) sprintf(line1,"%s|%s",line1,F4);
  411.  
  412.         sprintf(line1,"%s|%s",line1,LP);
  413.         sprintf(line1,"%s -P%s",line1,printer);
  414.         sprintf(line1,"%s%s",line1,spool);
  415.         if (LO != NULL) sprintf(line1,"%s %s",line1,LO);
  416.  
  417.         if (debug) 
  418.                 printf("line1 is \n  %s \n",line1);
  419.         else 
  420.             system(line1);
  421.  
  422.     }
  423.  
  424.  
  425. }
  426.  
  427.  
  428. /*----------------------------------------------------------------------------*
  429.  | Routine:    char  * operand (& argc,  & argv)
  430.  |
  431.  | Results:    returns address of the operand given with a command-line
  432.  |        option.  It uses either "-Xoperand" or "-X operand", whichever
  433.  |        is present.  The program is terminated if no option is present.
  434.  |
  435.  | Side Efct:    argc and argv are updated as necessary.
  436.  *----------------------------------------------------------------------------*/
  437.  
  438. char *operand(argcp, argvp)
  439. int * argcp;
  440. char ***argvp;
  441. {
  442.     if ((**argvp)[2]) return(**argvp + 2); /* operand immediately follows */
  443.     if ((--*argcp) <= 0) {            /* no operand */
  444.         fprintf(stderr,"ditroff: command-line option operand missing.\n");
  445.         exit(8);
  446.     }
  447.     return(*(++(*argvp)));            /* operand is next word */
  448. }
  449.  
  450.  
  451. char     *dgetstr();
  452.  
  453. /*----------------------------------------------------------------------------*
  454.  | Routine:    getditline(printer)
  455.  |
  456.  | Results:    gets the ditcap line for the printer specified
  457.  *----------------------------------------------------------------------------*/
  458.  
  459. getditline(device)
  460.     char    *device;
  461. {
  462.     int     status;
  463.  
  464.     if ((status = dgetent(ditline,device)) < 0) {
  465.          fprintf(stderr,"ditroff: can't open the ditcap file \n");
  466.          exit(1);
  467.     }
  468.     else if (status == 0) {
  469.          fprintf(stderr,"ditroff: can't find device %s in the ditcap file \n", device);
  470.          exit(1);
  471.     }
  472.     bp = ditbuf;
  473. }
  474.  
  475. /*----------------------------------------------------------------------------*
  476.  | Routine:    char *gettype (printer)
  477.  |
  478.  | Results:    returns the printer type by looking at the ditcap line
  479.  *----------------------------------------------------------------------------*/
  480.  
  481. char *
  482. gettype(device)
  483.     char    *device;
  484. {
  485.     char     *cp;
  486.  
  487.     if ((cp = dgetstr("ty", &bp)) == NULL) {
  488.          fprintf(stderr,"ditroff: no type specified for device %s in the ditcap file \n", device);
  489.          exit(1);
  490.     }
  491.     return (cp);
  492. }
  493.  
  494.  
  495. /*----------------------------------------------------------------------------*
  496.  | Routine:    ditoptions()
  497.  |
  498.  | Results:    gets the options from the ditcap line
  499.  *----------------------------------------------------------------------------*/
  500.  
  501. ditoptions()
  502. {
  503.     char *cp;
  504.  
  505.     FD = dgetstr("fd",&bp);
  506.  
  507.     TR = TROFF;
  508.     if (cp=dgetstr("tr",&bp)) TR = cp; 
  509.  
  510.     LO = dgetstr("lo",&bp);
  511.     F1 = dgetstr("f1",&bp);
  512.     F2 = dgetstr("f2",&bp);
  513.     F3 = dgetstr("f3",&bp);
  514.     F4 = dgetstr("f4",&bp);
  515.     OL = dgetstr("ol",&bp);
  516.     FA = dgetstr("fa",&bp);
  517.     OA = dgetstr("oa",&bp);
  518.     FT = dgetstr("ft",&bp);
  519.     OT = dgetstr("ot",&bp);
  520.     FP = dgetstr("fp",&bp);
  521.     OP = dgetstr("op",&bp);
  522.     PV = dgetstr("pv",&bp);
  523.  
  524.     LP = LPR;
  525.     if (cp=dgetstr("lp",&bp)) LP = cp; 
  526.  
  527.     TB = DTBL;
  528.     if (cp=dgetstr("tb",&bp)) TB = cp; 
  529.  
  530.     EQ = DEQN;
  531.     if (cp=dgetstr("eq",&bp)) EQ = cp; 
  532.  
  533.     GR = GRN;
  534.     if (cp=dgetstr("gr",&bp)) GR = cp; 
  535.  
  536.     PI = PIC;
  537.     if (cp=dgetstr("pi",&bp)) PI = cp; 
  538.  
  539.     RF = REFER;
  540.     if (cp=dgetstr("rf",&bp)) RF = cp; 
  541.  
  542.     ID = IDEAL;
  543.     if (cp=dgetstr("id",&bp)) ID = cp; 
  544.  
  545.     SE = dgetstr("se",&bp);
  546. }
  547. @
  548.